package com.ww.springboot.rabbitmq.controller;

import cn.hutool.core.lang.func.Func;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.GetResponse;
import com.ww.springboot.rabbitmq.dto.ExpServiceMsg;
import com.ww.springboot.rabbitmq.mq.ExchangeEnum;
import com.ww.springboot.rabbitmq.mq.MessageSender;
import com.ww.springboot.rabbitmq.mq.QueueConstants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.DefaultMessagePropertiesConverter;
import org.springframework.amqp.rabbit.support.MessagePropertiesConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.nio.charset.StandardCharsets;


/**
 * @author ZH500
 */
@RestController
@RequestMapping("mq2")
public class MqController2 {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private MessageSender messageSender;

    private final MessagePropertiesConverter propertiesConverter = new DefaultMessagePropertiesConverter();


    private Integer i = 0;

    @RequestMapping("send")
    public String find(@RequestBody ExpServiceMsg expServiceMsg) {
        if (!StringUtils.isEmpty(expServiceMsg.getMessage())) {
            //expServiceMsg.setMessage(expServiceMsg.getMessage() + System.currentTimeMillis());
            rabbitTemplate.convertAndSend("exchange.000027.incrSyncListing", "routingKey.000027.incrSyncListing", expServiceMsg.getMessage());
        }
        return "成功";
    }

    @RequestMapping("consumer")
    public String consumer() throws InterruptedException {
        new Thread(() -> {
            while (true){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                rabbitTemplate.execute(channel -> {
                    GetResponse resp = channel.basicGet("queue.000000.incrSyncListing", false);
                    if (resp == null) {
                        return null;
                    }
                    Message message = new Message(resp.getBody(), propertiesConverter.toMessageProperties(
                            resp.getProps(), resp.getEnvelope(), StandardCharsets.UTF_8.name()));
                    System.out.println(new String(message.getBody()) + Thread.currentThread());
                    Thread.sleep(10000);
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    return null;
                });
            }
        }).start();
        new Thread(() -> {
            while (true){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                rabbitTemplate.execute(channel -> {
                    GetResponse resp = channel.basicGet("queue.000000.incrSyncListing", false);
                    if (resp == null) {
                        return null;
                    }
                    Message message = new Message(resp.getBody(), propertiesConverter.toMessageProperties(
                            resp.getProps(), resp.getEnvelope(), StandardCharsets.UTF_8.name()));
                    System.out.println(new String(message.getBody()) + Thread.currentThread());
                    Thread.sleep(10000);
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    return null;
                });
            }
        }).start();
        new Thread(() -> {
            while (true){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                rabbitTemplate.execute(channel -> {
                    GetResponse resp = channel.basicGet("queue.000000.incrSyncListing", false);
                    if (resp == null) {
                        return null;
                    }
                    Message message = new Message(resp.getBody(), propertiesConverter.toMessageProperties(
                            resp.getProps(), resp.getEnvelope(), StandardCharsets.UTF_8.name()));
                    System.out.println(new String(message.getBody()) + Thread.currentThread());
                    Thread.sleep(10000);
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    return null;
                });
            }
        }).start();
        new Thread(() -> {
            while (true){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                rabbitTemplate.execute(channel -> {
                    GetResponse resp = channel.basicGet("queue.000000.incrSyncListing", false);
                    if (resp == null) {
                        return null;
                    }
                    Message message = new Message(resp.getBody(), propertiesConverter.toMessageProperties(
                            resp.getProps(), resp.getEnvelope(), StandardCharsets.UTF_8.name()));
                    System.out.println(new String(message.getBody()) + Thread.currentThread());
                    Thread.sleep(10000);
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    return null;
                });
            }
        }).start();
        new Thread(() -> {
            while (true){
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                rabbitTemplate.execute(channel -> {
                    GetResponse resp = channel.basicGet("queue.000000.incrSyncListing", false);
                    if (resp == null) {
                        return null;
                    }
                    Message message = new Message(resp.getBody(), propertiesConverter.toMessageProperties(
                            resp.getProps(), resp.getEnvelope(), StandardCharsets.UTF_8.name()));
                    System.out.println(new String(message.getBody()) + Thread.currentThread());
                    Thread.sleep(10000);
                    channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
                    return null;
                });
            }
        }).start();


        int i = 0;
        while (true){
            Thread.sleep(10);
            if(i == 1){
                break;
            }
        }
        return "成功";
    }
}
